package com.haruhinav.service.impl;

import com.haruhinav.entity.Address;
import com.haruhinav.entity.AddressType;
import com.haruhinav.entity.extend.AddressEX;
import com.haruhinav.entity.vo.ResultVO;
import com.haruhinav.mapper.AddressMapper;
import com.haruhinav.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AddressServiceImpl implements AddressService {

    @Autowired
    private AddressMapper addressMapper;


    //所有地址 一对一
    @Override
    public ResultVO getAllAddress(AddressEX addressEX) {

//        sql需要的是从第几条开始，往后查询多少条
//        前台过来的是页码，和每页显示数
        addressEX.setPage((addressEX.getPage()-1)*addressEX.getPageSize());


        ResultVO result = new ResultVO();
        Map<String,Object> resultMap = new HashMap<>();

        resultMap.put("list",addressMapper.getAllAddress(addressEX));
        resultMap.put("count",addressMapper.countAddress());
        result.setData(resultMap);
        result.setCode(0);
        result.setMsg("OK");
        return result;
    }

    //所有地址 根据类型 一对多
    @Override
    public List<AddressType> getAllAddressByType() {
        return addressMapper.getAllAddressByType();
    }

    @Override
    public List<AddressType> getAllAddressByTypeV2() {
        return addressMapper.getAllAddressByTypeV2();
    }

    //根据类型id查找类型
    @Override
    public AddressType findAddressTypeById(int id) {
        return addressMapper.findAddressTypeById(id);
    }

    @Override
    public List<AddressType> queryAllAddressType(AddressType addressType) {


        return addressMapper.queryAllAddressType(addressType);
    }

    @Override
    public ResultVO updateState(Integer state, Integer id) {
        ResultVO result = new ResultVO();

        if(addressMapper.updateState(state,id)>0){
            result.setCode(0);
            result.setMsg("修改成功");
            result.setData(id);
            return result;
        }
        result.setCode(-1);
        result.setMsg("修改失败");
        result.setType("error");

        return result;
    }

    @Override
    public ResultVO addNavAddress(Address address) {

        ResultVO result = new ResultVO();
        int count =addressMapper.insertAddress(address);

        if(count>0 && address.getId()!=0){
            result.setCode(0);
            result.setMsg("添加成功");
            result.setData(address);
            return  result;
        }
        result.setCode(-1);
        result.setMsg("失败");
        result.setType("error");

        return result;
    }

    @Override
    public Address findAddressById(Integer id) {
        return addressMapper.findAddressById(id);
    }

    @Override
    public ResultVO updateAddress(Address address) {
        address.setFlag(address.getFlag()==null ||  address.getFlag()==0 ? 0 : 1);
        ResultVO result = new ResultVO();

        if(addressMapper.updateAddress(address)>0){
            result.setCode(0);
            result.setMsg("修改成功");
            result.setData(address);
            return result;
        }
        result.setCode(-1);
        result.setMsg("修改失败");
        result.setType("error");
        return result;
    }

    @Override
    public ResultVO removeAddressById(Integer id) {
        ResultVO result = new ResultVO();
        if(addressMapper.removeAddressById(id)>0){
            result.setCode(0);
            result.setMsg("删除成功");
            result.setData(id);
            return result;
        }
        result.setCode(-1);
        result.setMsg("删除失败");
        result.setData(id);
        result.setType("error");
        return result;
    }

   // @Override
//    public List<AddressType> queryAllType(AddressType addressType) {
//
//        return addressMapper.queryAllType(addressType);
//    }


    @Override
    @Transactional
    public ResultVO insertAddressType(AddressType addressType) {
        ResultVO result = new ResultVO();
//        List<AddressType> ats = addressMapper.findATByGTNewIndex(addressType.getAtIndex());
//        if(ats.size()>0)
//            ats.forEach(at->addressMapper.updateAddressTypeIndex(at.getAtIndex()+1,at.getId()));
//        添加之前，把比预添加数据index大的index数据都+1
        addressMapper.updateAddressTypeIndexPutOne(addressType.getAtIndex());

        int count = addressMapper.insertAddressType(addressType);
        if(count > 0 && addressType.getId() != 0){
            result.setCode(0);
            result.setData(addressType);
            result.setMsg("添加成功");
            return result;
        }
        result.setCode(-1);
        result.setType("error");
        result.setMsg("添加失败");
        return result;
    }

    @Override
    public ResultVO updateAddressTypeColor(Map repMap, String mark) {
        ResultVO result = new ResultVO();
        AddressType addressType = new AddressType();
        addressType.setLabelColor(null==repMap.get("labelColor") ? null : repMap.get("labelColor").toString());
        addressType.setTagColor(null==repMap.get("tagColor") ?null:repMap.get("tagColor").toString());
        addressType.setId(Integer.parseInt(repMap.get("id").toString()));

        if("tag".equals(mark)){
            if(addressMapper.updateAddressTypeTagColoe(addressType.getTagColor(),addressType.getId())>0){
                result.setCode(0);
                result.setData(addressType.getTagColor());
                result.setMsg("修改成功");
                return result;
            }
            result.setCode(-1);
            result.setType("error");
            result.setMsg("修改失败");
            return result;
        }else if("label".equals(mark)){

            if(addressMapper.updateAddressTypeLabelColoe(addressType.getLabelColor(),addressType.getId())>0){

                result.setCode(0);
                result.setData(addressType.getLabelColor());
                result.setMsg("修改成功");
                return result;
            }
            result.setCode(-1);
            result.setType("error");
            result.setMsg("修改失败");
            return result;
        }
        result.setCode(-2);
        result.setType("warning");
        result.setMsg("标记无效");
        return result;
    }

    @Override
    @Transactional
    public ResultVO removeAddressTypeById(AddressType addressType) {
        ResultVO result = new ResultVO();
//        先删除这个类型下的地址
        addressMapper.delAddressByTypeId(addressType.getId());
//        删除之前，先把它后面的index-1
        addressMapper.updateAddressTypeIndexCutOne(addressType.getAtIndex());

//      这个类型下没有地址，直接删除该类型
        if(addressMapper.removeAddressTypeById(addressType.getId())>0){
            result.setCode(0);
            result.setMsg("删除类型成功");
            result.setData(addressType);
        }else{
            result.setCode(-1);
            result.setMsg("删除类型失败");
            result.setType("error");
            result.setData(addressType);
        }
        return result;
    }

    @Override
    public ResultVO updateAddressType(AddressType addressType) {
        ResultVO result = new ResultVO();
//        先判断旧的index和预修改的indx是否一致
        AddressType oldAddressType = addressMapper.findAddressTypeById(addressType.getId());//找到旧的地址类型数据
        if(addressType.getAtIndex()>oldAddressType.getAtIndex()){
//      新的index变大，排序往后 区间内index-1
            addressMapper.indexBiggerCutOne(addressType.getAtIndex(),oldAddressType.getAtIndex());

        }else if(addressType.getAtIndex()<oldAddressType.getAtIndex()){
//      新的index变小，排序往前 区间内index+1
            addressMapper.indexSmallerPutOne(addressType.getAtIndex(),oldAddressType.getAtIndex());
        }
//        执行修改
        if( addressMapper.updateAddressType(addressType)>0){
            result.setCode(0);
            result.setMsg("修改成功");
            result.setData(addressType);
            return result;
        }
        result.setCode(-1);
        result.setMsg("修改失败");
        result.setType("error");
        result.setData(addressType);
        return result;
    }
}
